package com.drama.outtake.controller;

import com.drama.outtake.common.Result;
import com.drama.outtake.domain.Address;
import com.drama.outtake.service.IAddressService;
import com.drama.outtake.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : 李先均
 * @Date 2022/12/22 21:32
 */
@RestController
@RequestMapping("/address")
@RequiredArgsConstructor
public class AddressController {

    private final IAddressService addressService;
    private final IUserService userService;

    @PostMapping
    public Result<Object> create(@RequestBody Address address) {
        addressService.create(address);
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        addressService.delete(id);
        return Result.ok();
    }

    @PutMapping
    public Result<Object> update(@RequestBody Address address) {
        addressService.update(address);
        return Result.ok();
    }

    @GetMapping()
    public Result<List<Address>> getAddress() {
        Long userId = userService.getMyUserId();
        return Result.ok(addressService.getAddressesByUserId(userId));
    }

    @GetMapping("/{id}")
    public Result<Address> getAddressById(@PathVariable("id") Long id) {
        if (id.equals(-1L)) {
            Long userId = userService.getMyUserId();
            return Result.ok(addressService.getDefaultAddressesByUserId(userId));
        }
        return Result.ok(addressService.getAddressById(id));
    }

    @GetMapping("/default")
    public Result<Address> getDefaultAddress() {
            Long userId = userService.getMyUserId();
            return Result.ok(addressService.getDefaultAddressesByUserId(userId));
    }

}
